package com.tsky.system.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tsky.casing.QueryBean;
import com.tsky.casing.action.BackAction;
import com.tsky.common.ActionUtil;
import com.tsky.common.BeanUtil;
import com.tsky.system.entity.Menu;
import com.tsky.system.json.JsonFormat;
import com.tsky.system.manager.MenuManager;
import com.tsky.system.query.MenuQuery;

/**
 * @describe: 菜单Action
 * @author: cainiao
 * @version 1.0
 * @time: 2010-2-26 上午09:18:39
 */
@Component("menuAction")
@Scope("prototype")
public class MenuAction extends BackAction {

	@Autowired
	private MenuManager entityManager;

	/* properties */
	private String id;
	private String name;
	private String status;
	private String url;// linkPage
	private String icon;// 小图标
	private int orderNO;
	private String property;

	private String ids;
	private String pid;
	private String roleid;// 配置的角色ID
	
	//  导航菜单
	public void getRoleMenuTree() throws Exception {
		String userid = this.getUserID();
		// String userid = "201005180355040062";
		List<?> menuList = entityManager.getRoleMenus(userid);
		String result = JsonFormat.getMenuTree(menuList);
		ActionUtil.ajaxWrite(result);
	}
	
	// 角色配置菜单
	public void getConfigMenuTree() {
		List<?> cfgList = entityManager.getConfigMenus(roleid);
		Object result = JsonFormat.getMenuTree(cfgList);
		ActionUtil.ajaxWrite(result);
	}
	
	// 空菜单
	public void getMenuTree() {
		List<?> menuList = entityManager.getMenus();
		String result = JsonFormat.getMenuTree(menuList);
		ActionUtil.ajaxWrite(result);
	}
	
	// 父菜单
	public void getPMenuTree(){
		List<?> menuList = entityManager.getPMenus();
		String result = JsonFormat.getMenuTree(menuList);
		ActionUtil.ajaxWrite(result);
	}
	
	public void grid() {
		QueryBean grid = new MenuQuery();
		BeanUtil.copyProperties(grid, this);
		
		entityManager.query(grid);

		String jsonResult = JsonFormat.menuToJson(grid);
		ActionUtil.ajaxWrite(jsonResult);
	}
	
	public void add() {
		Menu entity = new Menu();
		BeanUtil.copyProperties(entity, this);
		entity.setId(BeanUtil.getID());
		entity.setCreateTime(BeanUtil.getTimeStamp());
		
		// 父菜单
		if("0".equals(property)){
			Menu pMenu = new Menu();
			if(!BeanUtil.isEmpty(pid)){
				pMenu = entityManager.getEntity(Menu.class, pid);
				pMenu.setProperty("1");
				entity.setParent(pMenu);
			}
		}
		
		boolean result = entityManager.saveEntity(entity);
		ActionUtil.ajaxWrite(result);
	}

	public void delete() {
		String[] idset = ids.split(",");
		boolean result = entityManager.delEntity(Menu.class, idset);
		ActionUtil.ajaxWrite(result);
	}

	public String query() {
		Menu entity = entityManager.getEntity(Menu.class, id);
		ActionUtil.setToEL("menuEnt", entity);
		
		return "query";
	}

	public void update() {
		Menu entity = entityManager.getEntity(Menu.class, id);
		BeanUtil.copyProperties(entity, this);
		if(!BeanUtil.isEmpty(pid)){
			Menu pMenu = entityManager.getEntity(Menu.class, pid);
			entity.setParent(pMenu);
		}
		boolean result = entityManager.updateEntity(entity);
		ActionUtil.ajaxWrite(result);
	}

	/* spring ID */
	public MenuManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(MenuManager entityManager) {
		this.entityManager = entityManager;
	}
	
	/* getter and setter */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getOrderNO() {
		return orderNO;
	}

	public void setOrderNO(int orderNO) {
		this.orderNO = orderNO;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getRoleid() {
		return roleid;
	}

	public void setRoleid(String roleid) {
		this.roleid = roleid;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}
	
}
